<template>
  <div>
    <h1>交通局文件存储系统</h1>
    <div class="login-container">
        <div>
            <h2>登录</h2>
        </div>
        <div>
            <form @submit.prevent="login">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" v-model="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="password" v-model="password" required>
                </div>
                <div v-if="state" style="font-size: 12px;">
                    <img src="..\..\public\error.png">
                    用户名或密码错误
                </div>
                <div style="text-align: center;">
                    <button type="submit">登录</button>
                </div>
            </form>
        </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')
const state = ref(false)

const login = () => {
    if (username.value === 'admin' && password.value === '123456') {
        // 设置登录状态
        localStorage.setItem('isLoggedIn', 'true')
        // alert('登录成功')
        // 触发storage事件，通知App.vue更新状态
        window.dispatchEvent(new StorageEvent('storage', {
            key: 'isLoggedIn',
            oldValue: 'false',
            newValue: 'true',
            storageArea: localStorage
        }))
        state.value = false
        // 跳转到主页
        router.push('/home')
    } else {
        password.value = ''
        state.value = true
    }
}

// 页面加载时检查是否已登录
if (localStorage.getItem('isLoggedIn') === 'true') {
    router.push('/home')
}

</script>

<style scoped>
/* 确保登录页面占满父容器 */
.login-container {
    width: 100%;
    max-width: 400px;
    padding: 50px;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    background-color: white;
    margin: 20px auto;
}

img {
    width: 15px;
    height: 15px;
}

h1 {
    letter-spacing: 40px;
    margin-bottom: 25px;
    font-size: 38px;
    color: cadetblue;
}

/* 标题样式 */
h2 {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
    font-size: 24px;
    font-weight: 600;
}

/* 表单样式 */
form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* 表单组样式 */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* 标签样式 */
label {
    font-weight: 500;
    color: #555;
    font-size: 14px;
}

/* 输入框样式 */
input[type="text"],
input[type="password"] {
    padding: 12px 15px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 16px;
    transition: border-color 0.3s;
}

input[type="text"]:focus,
input[type="password"]:focus {
    border-color: #42b983;
    outline: none;
}

/* 按钮样式 */
button[type="submit"] {
    padding: 12px 20px;
    background-color: #42b983;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.3s;
    font-weight: 500;
}

button[type="submit"]:hover {
    background-color: #3aa876;
}

/* 响应式调整 */
@media (max-width: 480px) {
    .login-container {
        padding: 25px;
    }
}
</style>